package com.hyts.infodb.zuul.web;

import com.google.common.collect.Maps;
import com.hyts.infodb.bean.dto.BaseDTO;
import com.hyts.infodb.enums.SystemConfiguration;
import com.hyts.infodb.zuul.bean.ZuulRouteBO;
import com.hyts.infodb.zuul.bean.ZuulRouteVO;
import com.hyts.infodb.zuul.service.RefreshRouteService;
import com.hyts.infodb.zuul.service.ZuulService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * project-name:infodb
 * package-name:com.hyts.infodb.zuul.web
 * author:Libo/Alex
 * create-date:2019-05-22 22:16
 * copyright:libo-hyts-github
 * email:libo2dev@aliyun.com
 * description:此类主要用于: zuul 总体外置控制器
 */
@Controller
@RequestMapping("/route")
public class ZuulRouteController {


    @Autowired
    RefreshRouteService refreshRouteService;


    @Autowired
    ZuulHandlerMapping zuulHandlerMapping;


    @Autowired
    ZuulService zuulService;


    /**
     * 刷新操作
     * @return
     */
    @ResponseBody
    @GetMapping("/refresh")
    public String refresh(){
        try {
            refreshRouteService.refreshRoute();
        } catch (Exception e) {
            return "failure";
        }
        return "success";
    }

    @ResponseBody
    @GetMapping("/watch")
    public Map<String, Object> watch(){
        try {
            return zuulHandlerMapping.getHandlerMap();
        } catch (Exception e) {;
            return Maps.newHashMap();
        }
    }

    @PostMapping("/save")
    public String save(ZuulRouteBO zuulRouteBO){
        BaseDTO baseDTO = new BaseDTO();
        boolean flag = zuulService.save(zuulRouteBO);
        BaseDTO<?> result = flag?baseDTO.success(SystemConfiguration.MessageConfiguration.SAVE,"网关配置信息"):
                        baseDTO.failure(SystemConfiguration.MessageConfiguration.SAVE,"网关配置信息");
        refreshRouteService.refreshRoute();
        return "/list";
    }


    @GetMapping("/remove/{id}")
    public String remove(ZuulRouteBO zuulRouteBO){
        BaseDTO baseDTO = new BaseDTO();
        boolean flag = zuulService.remove(zuulRouteBO);
        BaseDTO<?> result = flag?baseDTO.success(SystemConfiguration.MessageConfiguration.REMOVE,"网关配置信息"):
                baseDTO.failure(SystemConfiguration.MessageConfiguration.REMOVE,"网关配置信息");
        refreshRouteService.refreshRoute();
        return "/list";
    }


    @ResponseBody
    @GetMapping("/search/{zuulName}")
    public ZuulRouteVO remove(@PathVariable("zuulName") String zuulName){
        ZuulRouteBO zuulRouteBO = new ZuulRouteBO();
        zuulRouteBO.setName(zuulName);
        ZuulRouteVO ZuulRouteVO = zuulService.find(zuulRouteBO);
        return ZuulRouteVO;
    }

    @ResponseBody
    @PostMapping("/validate")
    public BaseDTO validate(@RequestBody ZuulRouteBO zuulRouteBO){
        zuulService.validate(zuulRouteBO);
        return new BaseDTO().success(SystemConfiguration.MessageConfiguration.SAVE);
    }




}
